package common.service.impl.jedis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.stereotype.Service;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import common.service.JedisClient;

import java.util.*;

/**
 * 通过数据库初始化 Redis缓存
 */
@Service("jedisClient")
public class ManualRedis implements JedisClient {

    //单机
    public static JedisPool jedisPool;

    //集群
    public static JedisCluster jedisCluster;

    /**
     * 初始化 redis(单机，集群)
     * @param params   host和port和密码
     */
    public int init(Map<String, Object> params) {
        try{
            if(params.get("REDIS_TYPE").equals(1)){
                if(params.get("REDIS_PWD")!=null&&!params.get("REDIS_PWD").equals("")){
                    jedisPool=new JedisPool(new GenericObjectPoolConfig(),params.get("REDIS_HOST").toString(),(Integer)params.get("REDIS_HOST"),3000,params.get("REDIS_PWD").toString());
                }else{
                    jedisPool=new JedisPool(new GenericObjectPoolConfig(),params.get("REDIS_HOST").toString(),(Integer)params.get("REDIS_HOST"),3000);
                }


            }else if(params.get("REDIS_TYPE").equals(2)){
                Set<HostAndPort> nodes=new HashSet<>();
                 String[] ports=params.get("REDIS_PORT").toString().split(",");
                for (String port:ports) {
                    nodes.add(new HostAndPort(params.get("REDIS_HOST").toString(), Integer.parseInt(port)));
                }
                if(params.get("REDIS_PWD")!=null&&!params.get("REDIS_PWD").equals("")){
                    jedisCluster=new JedisCluster(nodes,5000,3000,5000,params.get("REDIS_PWD").toString(),new GenericObjectPoolConfig());
                }else{
                    jedisCluster=new JedisCluster(nodes,5000,3000,5000,new GenericObjectPoolConfig());
                }

            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
    public String set(String key, String value) {
        if(jedisCluster!=null){
            return jedisCluster.set(key, value);
        }else {
            Jedis jedis = jedisPool.getResource();
            String result = jedis.set(key, value);
            jedis.close();
            return result;
        }

    }

    public String get(String key) {
        if(jedisCluster!=null){
            return jedisCluster.get(key);
        }else {
            Jedis jedis = jedisPool.getResource();
            String result = jedis.get(key);
            jedis.close();
            return result;
        }

    }

    public Boolean exists(String key) {
        if(jedisCluster!=null){
            return jedisCluster.exists(key);
        }else {
            Jedis jedis = jedisPool.getResource();
            Boolean result = jedis.exists(key);
            jedis.close();
            return result;
        }

    }

    public Long expire(String key, int seconds) {
        if(jedisCluster!=null){
            return jedisCluster.expire(key, seconds);
        }else {
            Jedis jedis = jedisPool.getResource();
            Long result = jedis.expire(key, seconds);
            jedis.close();
            return result;
        }

    }

    public Long ttl(String key) {
        if(jedisCluster!=null){
            return jedisCluster.ttl(key);
        }else {
            Jedis jedis = jedisPool.getResource();
            Long result = jedis.ttl(key);
            jedis.close();
            return result;
        }

    }

    public Long incr(String key) {
        if(jedisCluster!=null){
            return jedisCluster.incr(key);
        }else {
            Jedis jedis = jedisPool.getResource();
            Long result = jedis.incr(key);
            jedis.close();
            return result;
        }

    }

    public Long hset(String key, String field, String value) {
        if(jedisCluster!=null){
            return jedisCluster.hset(key, field, value);
        }else {
            Jedis jedis = jedisPool.getResource();
            Long result = jedis.hset(key, field, value);
            jedis.close();
            return result;
        }

    }

    public String hget(String key, String field) {
        if(jedisCluster!=null){
            return jedisCluster.hget(key, field);
        }else {
            Jedis jedis = jedisPool.getResource();
            String result = jedis.hget(key, field);
            jedis.close();
            return result;
        }

    }

    public Long hdel(String key, String... field) {
        if(jedisCluster!=null){
            return jedisCluster.hdel(key, field);
        }else {
            Jedis jedis = jedisPool.getResource();
            Long result = jedis.hdel(key, field);
            jedis.close();
            return result;
        }

    }



    public JedisCluster getJedisCluster() {
        return jedisCluster;
    }

    public void setJedisCluster(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }



}
